package com.molichuxing.services.business.service.impl;

import com.molichuxing.services.business.dto.response.VehicleBizDto;
import com.molichuxing.services.business.dto.response.VehicleModlemakeBizDto;
import com.molichuxing.services.business.dto.response.VehicleNameBizDto;
import com.molichuxing.services.business.service.VehicleBizService;
import com.molichuxing.services.business.service.VersionBizService;
import com.molichuxing.services.infrastructure.bean.VersionContentBean;
import com.molichuxing.services.infrastructure.bean.VersionMobileBean;
import com.molichuxing.services.infrastructure.convertor.Convert;
import com.molichuxing.services.infrastructure.dao.VersionContentMapper;
import com.molichuxing.services.infrastructure.dao.VersionInfoMapper;
import com.molichuxing.services.infrastructure.dao.VersionMobileMapper;
import com.molichuxing.services.infrastructure.dto.response.*;
import com.molichuxing.services.infrastructure.service.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 车辆服务实现类
 * </p>
 *
 */
@Service("versionBizService")
public class VersionizServiceImpl implements VersionBizService {

	private Logger logger = LoggerFactory.getLogger(this.getClass());

	@Resource
	private VersionMobileMapper versionMobileMapper;

	@Resource
	private VersionContentMapper versionContentMapper;

	@Resource
	private VersionInfoMapper versionInfoMapper;


	@Resource
	private VersionMobileService versionMobileService;

	@Override
	public VersionMobileDto getNewestMobile(String appVersion, String channel) {
		VersionMobileDto dto = new VersionMobileDto();
		try {
			dto.setIsNew(0);
			dto.setIsUpgrade(0);
			// 根据设备查询该设备的最新版本
			VersionMobileDto versionMobile = versionMobileService.findNewestByChannel(channel);
			if (versionMobile != null) {
				if (!appVersion.equalsIgnoreCase(versionMobile.getVersionNum())) {
					// 不是最新版本
					// 判断是否大于数据库最新版本号
					String versionNum = versionMobile.getVersionNum();
					String[] num = versionNum.split("[.]");
					String[] appNum = appVersion.split("[.]");
					if (Integer.parseInt(appNum[0]) > Integer.parseInt(num[0])) {
						// 大于数据库最新版本号,直接返回
						return dto;
					} else if (Integer.parseInt(appNum[0]) == Integer.parseInt(num[0])) {
						if (Integer.parseInt(appNum[1]) > Integer.parseInt(num[1])) {
							return dto;
						} else if (Integer.parseInt(appNum[1]) == Integer.parseInt(num[1])) {
							if (Integer.parseInt(appNum[2]) > Integer.parseInt(num[2])) {
								return dto;
							}
						}
					}

					dto.setIsNew(1);
					// 更新时间
					dto.setUpgradeDate(versionMobile.getUpgradeDate());
					// 版本号
					dto.setVersionNum(versionMobile.getVersionNum());
					// 更新路径
					dto.setVersionPath(versionMobile.getVersionPath());
					// 是否强制更新
					dto.setIsUpgrade(Integer.valueOf(versionMobile.getIsUpgrade()));
					if (dto.getIsUpgrade() == 0) {
						// 最新版本不强制更新，判断当前版本和最新版本之间是否有版本需要强制更新
						VersionMobileDto appVersionMobile = versionMobileService.findByVersionAndChannel(appVersion, channel);
						if (appVersionMobile != null) {
							// 当前版本到最新版本时间段是否有强制更新
							boolean isUpgrade = versionMobileService.findIsUpgradeBetweenDate(channel, appVersionMobile.getUpgradeDate(),versionMobile.getUpgradeDate());
							if (isUpgrade) {
								// 有强制更新
								dto.setIsUpgrade(1);
							}
						} else {
							// 无当前版本信息，强制更新到最新版本
							dto.setIsUpgrade(1);
						}
					}

					// 查询版本更新内容
					List<VersionContentDto> conList = versionMobileService.findContentByVersionId(versionMobile.getId());
					StringBuffer sb = new StringBuffer();
					if (!CollectionUtils.isEmpty(conList)) {
						for (VersionContentDto versionContent : conList) {
							sb.append(versionContent.getContent() + "\n");
						}
					}
					dto.setContent(sb.toString());
				}
			}

		} catch (Exception e) {
			logger.error("获取新版本失败", e);
		}
		return dto;
	}


}
